﻿// ActionScript file
/**
 * 提供倒帧等帧操作
 * 
 * @USAGE : 
 * MovieClipUtil.frameTo(目标MC,帧数,完成调用函数)
 * 
 * 
 * @Experience
 *  MovieClip添加自定义属性方�?
 */
package org.nickro.utils{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class MovieClipUtil extends EventDispatcher
	{
		public static var engines:Array = [];
		/**
		 * 将影片剪辑移动到指定�?
		 * @param mc		对象MovieClip
		 * @param frame 	指定的帧�?
		 * @param call  	到达指定帧数时调用的函数
		 * @param args  	回调函数的参数数�?
		 * 
		 */
		public static function frameTo(mc:MovieClip,frame:int,call:Function = null,..._args):void
		{
			if(engines.length > 0)
			{
				engines[0].removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				engines.shift();
			}
			var engine:Object = new MovieClip();
			engine.toFrame = frame;
			engine.target = mc;
			engine.callback = call;
			engine.params = _args;
			if(mc.currentFrame <= frame)
			{
				engine.dir = -1;
			}else
			{
				engine.dir = 1;
			}
			engines.push(engine);
			engine.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		private static function onEnterFrame(e:Event):void
		{
			var engine:Object = e.currentTarget;
			
			var mc:MovieClip = engine.target;
			if(mc.currentFrame == engine.toFrame)
			{
				if(engine.callback != null)
				{
					var params:Array = engine.params;
					if(params.length > 0)
					{
						engine.callback.apply(null,params);
					}else
					{
						engine.callback.apply(null,null);
					}
				}
				e.currentTarget.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			}else 
			{
				if(engine.dir == 1)
				{
					mc.prevFrame();
				}else
				{
					mc.nextFrame();
				}
			}
		}
	}
}